package com.wuyan.locationdog.ui.my

import android.content.Intent
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import androidx.fragment.app.viewModels
import com.amap.api.maps.offlinemap.OfflineMapActivity
import com.arcsoft.commonbase.base.BaseBindingFragment
import com.arcsoft.commonbase.bus.Bus
import com.arcsoft.commonbase.bus.bus_update_userinfo
import com.arcsoft.commonbase.ext.gone
import com.arcsoft.commonbase.ext.loadImageCircle
import com.arcsoft.commonbase.ext.visible
import com.blankj.utilcode.util.ActivityUtils
import com.wuyan.locationdog.databinding.MyFragmentBinding
import com.wuyan.locationdog.help.DogHelp
import com.wuyan.locationdog.help.LoginHelp
import com.wuyan.locationdog.ui.device.DeviceAddActivity
import com.wuyan.locationdog.ui.device.DeviceFenceListActivity
import com.wuyan.locationdog.ui.device.DogFilesListActivity
import com.wuyan.locationdog.ui.device.DogListActivity
import com.wuyan.locationdog.viewmodel.DeviceViewModel
import com.wuyan.locationdog.viewmodel.MyViewModel
import com.yzq.zxinglibrary.android.CaptureActivity
import com.yzq.zxinglibrary.bean.ZxingConfig
import com.yzq.zxinglibrary.common.Constant


class MyFragment : BaseBindingFragment<MyFragmentBinding>() {
    private val mViewModel: MyViewModel by viewModels()
    private val mDeviceViewModel: DeviceViewModel by viewModels()

    override fun lazyLoadData() {

        binding.apply {
            if (LoginHelp.isDeviceLogin) {
                layoutPhone.gone()
                btnDeviceShare.gone()

            } else {
                layoutPhone.visible()
            }
            btnOffLineMap.setOnClickListener {
                ActivityUtils.startActivity(OfflineMapActivity::class.java)
            }
            btnFenceManage.setOnClickListener {
                ActivityUtils.startActivity(DeviceFenceListActivity::class.java)
            }
            btnDeviceList.setOnClickListener {
                ActivityUtils.startActivity(DogListActivity::class.java)

            }
            btnTeamList.setOnClickListener {
                ActivityUtils.startActivity(TeamListActivity::class.java)
            }
            btnSet.setOnClickListener {
                ActivityUtils.startActivity(SetActivity::class.java)
            }
            btnMyOrder.setOnClickListener {
                ActivityUtils.startActivity(MyOrderActivity::class.java)
            }

            btnHelp.setOnClickListener {
                ActivityUtils.startActivity(MyFeedbackActivity::class.java)
            }
            btnAlarmSet.setOnClickListener {
                ActivityUtils.startActivity(MyAlarmSetActivity::class.java)
            }
            btnDeviceShare.setOnClickListener {
                ActivityUtils.startActivity(MyShareManageActivity::class.java)
            }
            btnMark.setOnClickListener {
                ActivityUtils.startActivity(MarkListActivity::class.java)
            }
            btnDeviceAdd.setOnClickListener {
                goCaptureActivity()
            }

            btnDogFiles.setOnClickListener {
                DogHelp.isGoDetail = true
                ActivityUtils.startActivity(DogFilesListActivity::class.java)
            }
        }
    }

    override fun onResume() {
        super.onResume()
        lazyLoadData()
    }

    private fun goCaptureActivity() {
        val intent = Intent(mActivity, CaptureActivity::class.java)

        val config = ZxingConfig()
        config.reactColor = com.arcsoft.commonbase.R.color.text_color_selected //设置扫描框四个角的颜色 默认为白色
//        config.frameLineColor = R.color.text_color_selected //设置扫描框边框颜色 默认无色
        config.scanLineColor = com.arcsoft.commonbase.R.color.text_color_selected //设置扫描线的颜色 默认白色
        config.isShowAddDevice = true
        intent.putExtra(Constant.INTENT_ZXING_CONFIG, config)
        startActivityForResult(intent, 1111)

    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        // 扫描二维码/条码回传
        if (requestCode == 1111 && resultCode == AppCompatActivity.RESULT_OK) {
            if (data != null) {
                val content = data.getStringExtra(Constant.CODED_CONTENT)
                if (content.toString() == "手动输入设备ID添加") {
                    val bundle = Bundle()
                    bundle.putString("scanText", "")
                    ActivityUtils.startActivity(bundle, DeviceAddActivity::class.java)
                } else {
                    val bundle = Bundle()
                    bundle.putString("scanText", content.toString())
                    ActivityUtils.startActivity(bundle, DeviceAddActivity::class.java)

                }
            }
        }
    }

    override fun loadNetData() {
        super.loadNetData()
        mViewModel.getBanner(hashMapOf())

        mViewModel.getMyInfo(hashMapOf())

        val map = HashMap<String, Any>()
//        map["groupId"] = GroupHelp.selectGroupBeanContent?.groupId?:""
        mDeviceViewModel.mapDeviceCount(map)
    }

    override fun observe() {
        super.observe()
        mViewModel.error.observe(this) {
//            binding.includedEmpty.setEmptyViewConfig("网络异常", "请检查网络设置或稍后重试", com.arcsoft.commonbase.R.mipmap.empty_net) {
//                loadNetData()
//            }
        }
        Bus.observe<String>(bus_update_userinfo, this) {
            mViewModel.getMyInfo(hashMapOf())
        }
        mViewModel.myInfoBean.observe(this) {
            binding.apply {
                tvNickName.text = it.content.nickName ?: it.content.userPhone
                tvSubTitle.text = it.content.userPhone

                ivHead.loadImageCircle(it.content.avatarDTO.fileUrl)
            }

        }
        mViewModel.getBannerBean.observe(this) {


        }


        mDeviceViewModel.deviceCountBean.observe(this) {

            binding.apply {
                val totalSize =
                    it.content.onlineCount + it.content.offlineCount + it.content.expiredCount + it.content.inactiveCount

            }

        }
    }

}